package com.yzh.springboot_rbac.controller;

import com.yzh.springboot_rbac.annotation.IpLimiter;
import com.yzh.springboot_rbac.entity.User;
import com.yzh.springboot_rbac.utils.AjaxResult;
import com.yzh.springboot_rbac.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;




/**
 * 测试RedisController
 */
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisUtil redisUtil;
    private static final String MESSAGE = "请求失败,你的IP访问太频繁";
    @GetMapping("save")
    @IpLimiter(ipAdress = "127.0.0.1", limit = 5, time = 30, message = MESSAGE)
    public String save(@RequestParam("key") String key, @RequestParam("value") String value){
        redisTemplate.opsForValue().set(key, value);
        return "success";
    }
    /**
     * 添加实体类类型数据到redis中
     */
    @PostMapping("/add")
    public AjaxResult addKeyAndValue(@RequestBody User user) {
//        redisUtil.set("userId" + user.getUserId(), "user{" + user + "}");
        redisTemplate.opsForValue().set(user.getUserName(), user);
        return AjaxResult.success();
    }
}
